<?php
include_once("DB.php");

class Base
{
	private $db = null;

	public function save($table)
	{
		if($this->db == null)
			$this->db = new DB();

		$obj = new ReflectionObject($table);
		$name = $obj->getName();
		$metadata = array();

		foreach($obj->getProperties() as $prop)
		{
			$propName = $prop->getName();
			$prop->setAccessible(true);
			$metadata[$propName] = $prop->getValue($table);
		}

		//$select = $this->build_select($name, $metadata);
		$update = $this->build_update($name, $metadata);

		echo "Update = $update<br />";
		//$this->db->execute($update);
	}
	
	private function build_select($name, $metadata)
	{
		$statement = "";
		$select = new Select();
		$select->table = $name;

		foreach($metadata as $key=>$val)
		{
			$field = new Field();
			
			if(strpos($key, "_") === 0)
			{
				$key = substr_replace($key, "", 0, 1);
			}
			
			$field->attr($key)->val($val);
			$select->fields->append($field);
		}

		return $select->out();
	}
	
	private function build_update($name, $metadata)
	{
		$statement = "";
		$update = new Update();
		$update->table = $name;

		foreach($metadata as $key=>$val)
		{
			$field = new Field();
			
			if(strpos($key, "_") === 0)
			{
				$key = substr_replace($key, "", 0, 1);
			}
			
			$field->attr($key)->val($val);
			$update->fields->append($field);
		}

		return $update->out();
	}
	
	public function __construct()
	{
		$this->db = new DB();
	}
	
	/*private function __destruct()
	{
		unset($db);
	}*/
}

?>
